@extends("layout.admin")

@section("title")
    修改用户
@endsection

@section("content")
    {{--修改样式--}}
    <style>
        .layui-input-width {
            width: 50% !important;
        }

        .layui-treeSelect {
            width: 29% !important;
        }
        .layui-form-switch {
            border-radius: 6px !important;
        }
    </style>
    {{--添加--}}
    <form id="create_from" style="margin-top: 15px;" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" id="name" name="name" autocomplete="off" placeholder="账号"
                       class="layui-input layui-input-width"
                       @if($user->id == \Auth::guard(session('guard'))->user()->id) disabled
                       @endif value="{{$user->name}}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">真名</label>
            <div class="layui-input-block">
                <input type="text" id="real_name" name="real_name" autocomplete="off" placeholder="真名"
                       class="layui-input layui-input-width"
                       @if($user->id == \Auth::guard(session('guard'))->user()->id) disabled
                       @endif value="{{$user->real_name}}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" id="email" name="email" autocomplete="off" placeholder="邮箱"
                       class="layui-input layui-input-width" value="{{$user->email}}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电话号码</label>
            <div class="layui-input-block">
                <input type="number" id="mobile" name="mobile" autocomplete="off" placeholder="电话"
                       class="layui-input layui-input-width" value="{{$user->mobile}}">
            </div>
        </div>
        @if(getCurrentSiteId()==0)
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">站点选择</label>
                    <div class="layui-input-block">
                        <select id="site_id" name="site_id" disabled lay-verify="required" lay-search="">
                            @foreach($sites as $site)
                                @if($user->site_id==$site["id"])
                                    <option value="{{$site['id']}}" selected>{{$site["name"]}}</option>
                                @endif
                                @if($user->site_id==0)
                                    <option value="0" selected>全站</option>
                                @endif
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        @endif

        @if(session('guard')=="admin")
        <div class="layui-form-item">
            <label class="layui-form-label">组织机构</label>
            <div class="layui-input-block">
                <div id="org" style="width: 50%"></div>
            </div>
        </div>
        @endif

        <div class="layui-form-item">
            <label class="layui-form-label"></label>

            <div class="layui-input-inline" style="width: 90px;">
                <img class="layui-upload-img" style="width: 80px;height: 80px"
                     src=@if($user->avatar==null) "{{asset('images/temp.jpeg')}}" @else "{{asset($user->avatar)}}
                " @endif id="showimg" name="showimg">
                <p id="errorText"></p>
                <input id="avatar" name="avatar" style="display: none;"/>
            </div>

            <button type="button" class="layui-btn layui-input-inline" style="margin-top: 25px;" id="upimg">上传头像
            </button>


        </div>
        @if(session('guard')=='system')
        <div class="layui-form-item" style="width: 20% !important;">
            <label class="layui-form-label">守卫</label>
            <div class="layui-input-block">
                <select id="type" disabled>
                    {!! Code::getOptionsByCode('GUARD','',$user->guard_name) !!}
                </select>
            </div>
        </div>
        @endif

        @can("admin.user.onoff")
            <div class="layui-form-item">
                <label class="layui-form-label">账号状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" @if($user->id == \Auth::guard(session('guard'))->user()->id) disabled
                           @endif @if($user->is_locked==0) checked="" @endif id="is_locked" name="is_locked"
                           lay-skin="switch" lay-filter="switchLocked"
                           lay-text="ON|OFF">
                </div>
            </div>
        @endcan

        @can("system.user.onoff")
            <div class="layui-form-item">
                <label class="layui-form-label">账号状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" @if($user->id == \Auth::guard(session('guard'))->user()->id) disabled
                           @endif @if($user->is_locked==0) checked="" @endif id="is_locked" name="is_locked"
                           lay-skin="switch" lay-filter="switchLocked"
                           lay-text="ON|OFF">
                </div>
            </div>
        @endcan
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="user_store">立即提交</button>
                <button type="button" class="layui-btn layui-btn-normal"
                        onclick='window.location.href="javascript:history.go(-1)"'>返回
                </button>
            </div>
        </div>
    </form>
@endsection

@section('script')
    <script src="{{asset('layui/modules/xm-select.js')}}"></script>
    <script src="{{asset('js/tree/select.js')}}"></script>
    <script>
        var idx = -1;
        layui.use(['form', 'table', 'upload','xmSelect'], function () {
            var form = layui.form;
            var upload = layui.upload;
            var xmSelect=layui.xmSelect;

            //监听提交
            form.on('submit(user_store)', function (data) {
                let updata = JSON.parse(JSON.stringify(data.field));
                updata._token = '{{csrf_token()}}';
                $.ajax({
                    url: '{{session("guard")=="system"?route("system.user.update",["id"=>$user->id]):route("admin.user.update",["id"=>$user->id])}}',
                    data: updata,
                    method: "POST",
                    success: function (res) {
                        if (res.error > 0) {
                            //错误框
                            layer.msg(res.msg, {
                                anim: 6,
                                icon: 5,
                                time: 3000
                            });
                            return false;
                        }
                        layer.msg(res.msg, {icon: 6});
                        location.reload();
                    }

                });
            });

            //上传图片
            upload.render({
                elem: '#upimg'
                , url: '{{session("guard")=="system"?route("system.user.upload"):route("admin.user.upload")}}'
                , method: "POST"
                , data: {"_token": "{{csrf_token()}}"}
                , field: "imgup"
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#showimg').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#avatar").val(res.data["path"]);
                    return layer.msg('上传成功', {icon: 6});
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var errorText = $('#errorText');
                    errorText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    errorText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });

            let params=selectTreeParams('#org','org_id','请选择',"{{$org_id}}",JSON.parse('{!! $orgs !!}'),(res)=>{});
            xmSelect.render(params)
        });
    </script>
@endsection
